package com.example.yoho.mvp.view.activity;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;


public class IndexView extends View {

    private int itemWidth;
    private int itemHeight;
    private Paint paint;
    private String[] words = {"A", "B", "C", "D", "E", "F", "G", "H", "I",
            "J", "K","L","M","N","O","P","Q","R","S","T"};


    public IndexView(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
        paint.setTextSize(40);  //字母字体大小
        paint.setColor(Color.RED); //默认设置画笔是白色
        paint.setAntiAlias(true); //设置抗锯齿
        paint.setTypeface(Typeface.DEFAULT_BOLD);//设置粗体字

    }


    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        itemWidth = getMeasuredWidth();
        itemHeight = getMeasuredHeight() / words.length;  //words.length是26。除以26
    }

    private int touchIndex = -1; //默认字母的下标位置

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (int i = 0; i < words.length; i++) {
            if (touchIndex == i) {
                //设置为灰色(按下去的时候)
                paint.setColor(Color.WHITE);
            } else {  //没有-1走下面代码
                //设置为白色(松开)
                paint.setColor(Color.GRAY);
            }

            String word = words[i];//若取值是A

            Rect rect = new Rect();  //矩形
            //画笔
            paint.getTextBounds(word, 0, 1, rect);  // 0, 1指的是取一个字母

            //字母的高和宽
            int wordWidth = rect.width();
            int wordHeight = rect.height();

            //计算每个字母在视图上的坐标位置
            float wordX = itemWidth / 2 - wordWidth / 2;
            float wordY = itemHeight / 2 + wordHeight / 2 + i * itemHeight;  ///i * itemHeight是指往下绘制字母

            canvas.drawText(word, wordX, wordY, paint);
        }
    }

    private OnIndexChangeListener onIndexChangeListener;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
                float Y = event.getY();
                int index = (int) (Y/itemHeight); //字母索引
                if(index != touchIndex){

                    touchIndex = index;  //当前的索引位置
                    invalidate(); //强制绘制会导致OnDraw()方法执行

                    if (onIndexChangeListener != null && touchIndex < words.length){
                        if (touchIndex>=0)
                        onIndexChangeListener.onIndexChange(words[touchIndex]);
                    }
                }
                break;

            case MotionEvent.ACTION_UP:
                touchIndex = -1;  //还原
                invalidate();
                break;
        }
        return true;
    }


    public interface OnIndexChangeListener{
        /**
         * 当字母下标位置发生变化的时候回调
         * @param word 字母（A~Z）
         */
        void onIndexChange(String word); //没有返回值
    }
    public void setOnIndexChangeListener(OnIndexChangeListener onIndexChangeListener) {
        this.onIndexChangeListener = onIndexChangeListener;
    }


}
